package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Set;

@Slf4j
@Component
public class ScheduledTest {
    @Value("${reggie.images}")
    private String basePath;
    @Autowired
    private RedisTemplate redisTemplate;
    @Scheduled(cron = "* * * 1/1 * ? *")
    public void removeImg() {
        Set<String> difference = redisTemplate.opsForSet().difference("set1", "set2");
        if (difference!=null&&difference.size()>0){
            for (String fileName : difference) {
                File file = new File(basePath, fileName);
                file.delete();
            }
            redisTemplate.delete("set1");
            redisTemplate.delete("set2");
        }
        log.info("定时任务清除图片");
    }
}
